#Tables 3, A19, and A20

#This Script produces Table 3 in the main text and related results tables A19
#A20 in the Appendix
rm(list=ls());gc();gc();gc();gc();gc();gc();gc();gc()


library(fixest)
library(dplyr)
library(plm)


here::i_am("Scripts/Tab3A19A20Script.R")

library(here)


##First let's get the data####
analysis_data<-readRDS(here("Data", "analysis_data2.rds"))
            
 #create logged variables for sector-specific fundraising donations from individuals
 analysis_data$logfriamount_agriculture<-log(1+analysis_data$etotam_i_agri)
 analysis_data$logfriamount_construction<-log(1+analysis_data$etotam_i_constr)
 analysis_data$logfriamount_finance<-log(1+analysis_data$etotam_i_finan)
 analysis_data$logfriamount_education<-log(1+analysis_data$etotam_i_educ)
 analysis_data$logfriamount_business<-log(1+analysis_data$etotam_i_busin)
 analysis_data$logfriamount_transportation<-log(1+analysis_data$etotam_i_transp)
 analysis_data$logfriamount_law<-log(1+analysis_data$etotam_i_lawl)
 analysis_data$logfriamount_lawnl<-log(1+analysis_data$etotam_i_law)
 analysis_data$logfriamount_energy<-log(1+analysis_data$etotam_i_energy)
 analysis_data$logfriamount_health<-log(1+analysis_data$etotam_i_health)
 
 analysis_data$lognfriamount_agriculture<-log(1+analysis_data$totam_i_agri-analysis_data$etotam_i_agri)
 analysis_data$lognfriamount_construction<-log(1+analysis_data$totam_i_constr-analysis_data$etotam_i_constr)
 analysis_data$lognfriamount_finance<-log(1+analysis_data$totam_i_finan-analysis_data$etotam_i_finan)
 analysis_data$lognfriamount_education<-log(1+analysis_data$totam_i_educ-analysis_data$etotam_i_educ)
 analysis_data$lognfriamount_business<-log(1+analysis_data$totam_i_busin-analysis_data$etotam_i_busin)
 analysis_data$lognfriamount_transportation<-log(1+analysis_data$totam_i_transp-analysis_data$etotam_i_transp)
 analysis_data$lognfriamount_law<-log(1+analysis_data$totam_i_lawl-analysis_data$etotam_i_lawl)
 analysis_data$lognfriamount_lawnl<-log(1+analysis_data$totam_i_law-analysis_data$etotam_i_law)
 analysis_data$lognfriamount_energy<-log(1+analysis_data$totam_i_energy-analysis_data$etotam_i_energy)
 analysis_data$lognfriamount_health<-log(1+analysis_data$totam_i_health-analysis_data$etotam_i_health)
 
 
 #calculate sum of event fundraising donations from individuals in specific sector in given state-session (two-year periods)
 analysis_data<-analysis_data %>%
   group_by(stateyear2) %>%
   mutate(
     totals_etotam_i_agri=sum(etotam_i_agri),
     totals_etotam_i_educ=sum(etotam_i_educ),
     totals_etotam_i_lawl=sum(etotam_i_lawl),
     totals_etotam_i_transp=sum(etotam_i_transp),
     totals_etotam_i_constr=sum(etotam_i_constr),
     totals_etotam_i_energy=sum(etotam_i_energy),
     totals_etotam_i_finan=sum(etotam_i_finan),
     totals_etotam_i_health=sum(etotam_i_health),
     totals_etotam_i_busin=sum(etotam_i_busin))
 
 #calculate proportion of total event fundraising donations from individuals in specific sector in given state-session (two-year periods) received by legislator
 analysis_data<-analysis_data %>%
   mutate(
     pct_totals_agriculture=etotam_i_agri/totals_etotam_i_agri,
     pct_totals_education=etotam_i_educ/totals_etotam_i_educ,
     pct_totals_law=etotam_i_lawl/totals_etotam_i_lawl,
     pct_totals_transportation=etotam_i_transp/totals_etotam_i_transp,
     pct_totals_construction=etotam_i_constr/totals_etotam_i_constr,
     pct_totals_energy=etotam_i_energy/totals_etotam_i_energy,
     pct_totals_finance=etotam_i_finan/totals_etotam_i_finan,
     pct_totals_health=etotam_i_health/totals_etotam_i_health,
     pct_totals_business=etotam_i_busin/totals_etotam_i_busin
   )
 
 #create variable to be used when reshaping data
 analysis_data$an_etotam_i_health<-analysis_data$etotam_i_health
 analysis_data$an_etotam_i_agriculture<-analysis_data$etotam_i_agri
 analysis_data$an_etotam_i_transportation<-analysis_data$etotam_i_transp
 analysis_data$an_etotam_i_communications<-analysis_data$etotam_i_comms
 analysis_data$an_etotam_i_construction<-analysis_data$etotam_i_constr
 analysis_data$an_etotam_i_energy<-analysis_data$etotam_i_energy
 analysis_data$an_etotam_i_finance<-analysis_data$etotam_i_finan
 analysis_data$an_etotam_i_business<-analysis_data$etotam_i_busin
 analysis_data$an_etotam_i_education<-analysis_data$etotam_i_educ
 analysis_data$an_etotam_i_law<-analysis_data$etotam_i_lawl
 
 #reduce variables in dataset
 analysis_data<-analysis_data[,union(
   c(grep("an_etotam_i_",colnames(analysis_data)),
     grep("pct_totals_",colnames(analysis_data)),
     grep("lognfriamount_",colnames(analysis_data)),
     grep("logfriamount_",colnames(analysis_data)),
     grep("limit1",colnames(analysis_data)),
     grep("chairs_",colnames(analysis_data))),
   which(colnames(analysis_data)%in%c("party",
                                      "incmaj","statechamberyear2", "sen", "sab", "year", "yearsess2", "chamber","cleaned_candid",
                                      "candid","incumbent_legislature","statedistrictcycleid","nonelses","filerid2","total_ie_candid", "etotam_i", "leader")))
 ]
 #reduce variables in dataset
 if(length(which(colnames(analysis_data)=="pct_ampcte_communications"))>0){analysis_data<-analysis_data[,-which(colnames(analysis_data)=="pct_ampcte_communications")]}
 if(length(which(colnames(analysis_data)=="logiamount_nl"))>0){analysis_data<-analysis_data[,-which(colnames(analysis_data)=="logiamount_nl")]}
 if(length(which(colnames(analysis_data)=="lognfriamount_nl"))>0){analysis_data<-analysis_data[,-which(colnames(analysis_data)=="lognfriamount_nl")]}
 if(length(which(colnames(analysis_data)=="logfriamount_nl"))>0){analysis_data<-analysis_data[,-which(colnames(analysis_data)=="logfriamount_nl")]}
 if(length(which(colnames(analysis_data)=="logfriamount_transp"))>0){analysis_data<-analysis_data[,-which(colnames(analysis_data)=="logfriamount_transp")]}
 
 if(length(which(colnames(analysis_data)=="lognfriamount_lawnl"))>0){analysis_data<-analysis_data[,-which(colnames(analysis_data)=="lognfriamount_lawnl")]}
 if(length(which(colnames(analysis_data)=="logfriamount_lawnl"))>0){analysis_data<-analysis_data[,-which(colnames(analysis_data)=="logfriamount_lawnl")]} 
 if(length(which(colnames(analysis_data)=="logiamount_lawnl"))>0){analysis_data<-analysis_data[,-which(colnames(analysis_data)=="logiamount_lawnl")]}
 
 if(length(which(colnames(analysis_data)=="lognfriamount_transp"))>0){analysis_data<-analysis_data[,-which(colnames(analysis_data)=="lognfriamount_transp")]}
 
 if(length(which(colnames(analysis_data)=="an_etotam_i_communications"))>0){analysis_data<-analysis_data[,-which(colnames(analysis_data)=="an_etotam_i_communications")]}
 
 if(length(which(colnames(analysis_data)=="chairs_waysandmeans"))>0){analysis_data<-analysis_data[,-which(colnames(analysis_data)=="chairs_waysandmeans")]}
 if(length(which(colnames(analysis_data)=="chairs_rules"))>0){analysis_data<-analysis_data[,-which(colnames(analysis_data)=="chairs_rules")]}
 if(length(which(colnames(analysis_data)=="chairs_communicaton"))>0){analysis_data<-analysis_data[,-which(colnames(analysis_data)=="chairs_communicaton")]}
 if(length(which(colnames(analysis_data)=="chairs_other"))>0){analysis_data<-analysis_data[,-which(colnames(analysis_data)=="chairs_other")]}
 if(length(which(colnames(analysis_data)=="chairs_approp"))>0){analysis_data<-analysis_data[,-which(colnames(analysis_data)=="chairs_approp")]}
 
 
 #reduce variables in dataset
 analysis_data3<-analysis_data[,c(grep("^chairs_",colnames(analysis_data)),
                                  grep("^logfriamount_",colnames(analysis_data)),
                                  grep("^pct_totals_",colnames(analysis_data)), 
                                  grep("^an_etotam_i_",colnames(analysis_data)), 
                                  grep("cleaned_candid",colnames(analysis_data)),
                                  grep("sab",colnames(analysis_data)),
                                  grep("nonelses",colnames(analysis_data)),
                                  grep("sen",colnames(analysis_data)),
                                  which(colnames(analysis_data)=="year"),
                                  which(colnames(analysis_data)=="incmaj"),
                                  which(colnames(analysis_data)=="leader"),
                                  which(colnames(analysis_data)=="incumbent_legislature"),
                                  grep("party",colnames(analysis_data)),
                                  grep("limit1",colnames(analysis_data)),
                                  grep("filerid2",colnames(analysis_data)),
                                  grep("yearsess2",colnames(analysis_data)))]
 analysis_data3<-analysis_data3[,order(colnames(analysis_data3))]
 #reshaping dataset to long
 analysis_data4<-reshape(analysis_data3,
                         varying = list(
                           c(grep("^chairs_",colnames(analysis_data3))),
                           c(grep("^logfriamount_",colnames(analysis_data3))),
                           c(grep("^pct_totals_",colnames(analysis_data3))),
                           c(grep("^an_etotam_i_",colnames(analysis_data3)))
                         )
                         ,v.names = c("chairs_","logfriamount_","pct_totals_","an_etotam_i_"),
                         timevar="sector",times=sub("^logfriamount_", "", grep("^logfriamount_", colnames(analysis_data3), value = TRUE)),
                         idvar=c("cleaned_candid","yearsess2"),direction="long")
 
 analysis_data4$SectorChamberYears = as.factor(paste(analysis_data4$sector, analysis_data4$sab, analysis_data4$sen, analysis_data4$yearsess2, analysis_data4$nonelses,sep="-"))
 analysis_data4$CandIdYears = as.factor(paste(analysis_data4$cleaned_candid, analysis_data4$yearsess2, analysis_data4$nonelses,sep="-"))
 analysis_data4$CandIdSector = as.factor(paste(analysis_data4$cleaned_candid, analysis_data4$sector, sep="-"))
 
 analysis_data4$exc0<-as.numeric(analysis_data4$sen==1&analysis_data4$yearsess2==2019 & analysis_data4$sab=="WV" | 
                                   analysis_data4$sen==1&analysis_data4$yearsess2 ==2019 & analysis_data4$sab=="OH" | 
                                   analysis_data4$sen==1 & analysis_data4$yearsess2 ==2019 & analysis_data4$sab=="KY" | 
                                   analysis_data4$cleaned_candid==262471 | analysis_data4$filerid2=="NA")
 analysis_data4$exc01<-as.numeric(analysis_data4$sen==1&analysis_data4$yearsess2==2019 & analysis_data4$sab=="WV" | 
                                    analysis_data4$sen==1&analysis_data4$yearsess2 ==2019 & analysis_data4$sab=="OH" | 
                                    analysis_data4$sen==1 & analysis_data4$yearsess2 ==2019 & analysis_data4$sab=="KY" | 
                                    analysis_data4$cleaned_candid==262471 |
                                    analysis_data4$sab=="MI" & analysis_data4$year<2001 |
                                    analysis_data4$filerid2=="NA")
 analysis_data4$exc2<-as.numeric(analysis_data4$sen==1 & analysis_data4$yearsess2>2016 & analysis_data4$sab=="WV" | 
                                   analysis_data4$sen==1 & analysis_data4$yearsess2 >2016 & analysis_data4$sab=="OH" | 
                                   analysis_data4$sen==1 & analysis_data4$yearsess2 >2016 & analysis_data4$sab=="KY" | 
                                   analysis_data4$cleaned_candid==262471 |
                                   analysis_data4$sab=="MI" & analysis_data4$year<2004 |
                                   analysis_data4$filerid2=="NA")
 analysis_data4$exc2<-as.numeric(analysis_data4$sen==1 & analysis_data4$yearsess2>2016 & analysis_data4$sab=="WV" | 
                                   analysis_data4$sen==1 & analysis_data4$yearsess2 >2016 & analysis_data4$sab=="OH" | 
                                   analysis_data4$sen==1 & analysis_data4$yearsess2 >2016 & analysis_data4$sab=="KY" | 
                                   analysis_data4$cleaned_candid==262471 )
 
 analysis_data4$pct_totals_100=analysis_data4$pct_totals_*100	
 
 analysis_data5<-pdata.frame(analysis_data4[which(analysis_data4$exc0!=1&analysis_data4$limit1!=1),],index=c("cleaned_candid"))
 
 
 find_min_an_etotam_i<-min(analysis_data5$an_etotam_i_[which(analysis_data5$an_etotam_i_>0)])
 analysis_data5$an_etotam_i_norm<-analysis_data5$an_etotam_i_/find_min_an_etotam_i
 
 analysis_data5$logfriamount_w0<-analysis_data5$an_etotam_i_norm
 analysis_data5$logfriamount_w0[which(analysis_data5$logfriamount_w0>0)]<-log(analysis_data5$an_etotam_i_norm[which(analysis_data5$logfriamount_w0>0)])
 
 analysis_data5$logfriamount_w01<-analysis_data5$an_etotam_i_norm
 analysis_data5$logfriamount_w01[which(analysis_data5$logfriamount_w01>0)]<-log(analysis_data5$an_etotam_i_norm[which(analysis_data5$logfriamount_w01>0)])
 analysis_data5$logfriamount_w01[which(analysis_data5$logfriamount_w01==0)]<- -0.1
 
 analysis_data5$logfriamount_w1<-analysis_data5$an_etotam_i_norm
 analysis_data5$logfriamount_w1[which(analysis_data5$logfriamount_w1>0)]<-log(analysis_data5$an_etotam_i_norm[which(analysis_data5$logfriamount_w1>0)])
 analysis_data5$logfriamount_w1[which(analysis_data5$logfriamount_w1==0)]<- -1
 
 analysis_data5$logfriamount_w3<-analysis_data5$an_etotam_i_norm
 analysis_data5$logfriamount_w3[which(analysis_data5$logfriamount_w3>0)]<-log(analysis_data5$an_etotam_i_norm[which(analysis_data5$logfriamount_w3>0)])
 analysis_data5$logfriamount_w3[which(analysis_data5$logfriamount_w3==0)]<- -3
 
 
 analysis_data6<-analysis_data5
 
 #dropping singleton observations
 analysis_data6<-analysis_data6 %>% 
   group_by(SectorChamberYears) %>% 
   filter(n() > 1)
 analysis_data6<-analysis_data6 %>% 
   group_by(CandIdSector) %>% 
   filter(n() > 1)
 analysis_data6<-analysis_data6 %>% 
   group_by(CandIdYears) %>% 
   filter(n() > 1)
 
 m1<-feols(logfriamount_~chairs_,fixef=c("CandIdSector","CandIdYears","SectorChamberYears"),data=analysis_data6,cluster=c("cleaned_candid"))
 m2<-feols(pct_totals_100~chairs_,fixef=c("CandIdSector","CandIdYears","SectorChamberYears"),data=analysis_data6,cluster=c("cleaned_candid"))
 
 etable(m1,m2,tex=T,drop.section="stats",file=here("Results","Tab3.tex"),replace=T,
        title="Table 3: Effects of Chair Positions on Event Donations From Individuals in Regulated Sectors",
        digits=2,
        dict=c(chairs_="Chair of Committee Regulating Sector j",CandIdSector="Legislator-Sector FE",CandIdYears="Legislator-Session-Cohort FE",
               SectorChamberYears="Sector-Chamber-Session-Cohort FE"),headers=c("Ln Amount of Sector j's\nEvent Donations","% of Sector j's\nEvent Donations"))
 
 
 saveRDS(analysis_data6, here("Data", "FigureA12_data.rds"))
 #DATA AT THIS POINT IS ALSO USED FOR MATCHING ANALYSIS REFLECTED IN Figure A12
 #There, it is called as FigureA12_data.rds

 
 #Table A19: Effects of Chair Positions on Event Donations from Regulated Sectors, Calibrated Extensive Margin Value (TabA19.tex))
 analysis_data7<-analysis_data6[order(analysis_data6$cleaned_candid,analysis_data6$yearsess2,analysis_data6$sector),]
 
 m1<-feols(logfriamount_w0~chairs_,fixef=c("CandIdSector","CandIdYears","SectorChamberYears"),data=analysis_data7,cluster=c("cleaned_candid"))
 m2<-feols(logfriamount_w01~chairs_,fixef=c("CandIdSector","CandIdYears","SectorChamberYears"),data=analysis_data7,cluster=c("cleaned_candid"))
 m3<-feols(logfriamount_w1~chairs_,fixef=c("CandIdSector","CandIdYears","SectorChamberYears"),data=analysis_data7,cluster=c("cleaned_candid"))
 m4<-feols(logfriamount_w3~chairs_,fixef=c("CandIdSector","CandIdYears","SectorChamberYears"),data=analysis_data7,cluster=c("cleaned_candid"))
 
 etable(m1,m2,m3,m4,tex=T,file=here("Results", "TabA19.tex"),replace=T,
        title="Table A19: Effects of Chair Positions on Event Donations from Regulated Sectors, Calibrated Extensive Margin Value",
        digits=2,
        dict=c(chairs_="Chair of Committee Regulating Sector j",CandIdSector="Legislator-Sector FE",CandIdYears="Legislator-Session-Cohort FE",
               SectorChamberYears="Sector-Chamber-Session-Cohort FE"),
        headers=c("Weight=0", "Weight=0.1", "Weight=1", "Weight=3"))
 

 
 #Table A20: Effects of Chair Positions on Event Donations From Individuals in Regulated Sectors, By State (TabA20.tex)
 m1<-feols(logfriamount_~chairs_,fixef=c("CandIdSector","CandIdYears","SectorChamberYears"),data=analysis_data6[which(analysis_data6$sab=="KY"),],cluster=c("cleaned_candid"))
 m2<-feols(logfriamount_~chairs_,fixef=c("CandIdSector","CandIdYears","SectorChamberYears"),data=analysis_data6[which(analysis_data6$sab=="MI"),],cluster=c("cleaned_candid"))
 m3<-feols(logfriamount_~chairs_,fixef=c("CandIdSector","CandIdYears","SectorChamberYears"),data=analysis_data6[which(analysis_data6$sab=="OH"),],cluster=c("cleaned_candid"))
 m4<-feols(logfriamount_~chairs_,fixef=c("CandIdSector","CandIdYears","SectorChamberYears"),data=analysis_data6[which(analysis_data6$sab=="WV"),],cluster=c("cleaned_candid"))
 
 etable(m1,m2,m3,m4,tex=T,file=here("Results", "TabA20.tex"),replace=T,
        title="Table A20: Effects of Chair Positions on Event Donations From Individuals in Regulated Sectors, By State",
        digits=2,
        dict=c(chairs_="Chair of Committee Regulating Sector j",CandIdSector="Legislator-Sector FE",CandIdYears="Legislator-Session-Cohort FE",
               SectorChamberYears="Sector-Chamber-Session-Cohort FE"),
        headers=list(c("Ln Amount of Sector j's"),c("Event Donations"),c("KY","MI","OH","WV")))
 